#include "../../source/Server.hpp"

// 获取当前时间的字符串表示
std::string current_time()
{
    std::time_t now = std::time(nullptr);
    std::tm* localtime = std::localtime(&now);
    char timeStr[20];
    std::strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", localtime);
    return std::string(timeStr);
}

void OnMessage(Connection::ConnectionPtr conn, Buffer& buf)
{
    std::string read = buf.read_to_string();
    std::cout << "["  << current_time() << "]OnMessage: " << read << std::endl;
    conn->Send("[Server send]" + read);
}

void OnConnect(Connection::ConnectionPtr conn)
{
    std::cout << "have new connection , it id: " << conn->getId() << std::endl;
}

void CloseConnect(Connection::ConnectionPtr conn)
{
    std::cout << "CloseConnect , connection id: " << conn->getId() << std::endl;
}


int main()
{
    TcpServer server(8888, 2);

    server.SetMessageHandle(OnMessage);
    server.SetConnectHandle(OnConnect);
    server.SetCloseHandle(CloseConnect);

    server.EnableInactiveRelease(5);

    server.Start();


    return 0;
}
